const DEFAULT_IMAGE_URL = 'https://cdn-icons-png.flaticon.com/512/2748/2748558.png';
const STATUS_MAP = {
  wish: { icon: 'waiting', text: '想看' },
  collect: { icon: 'success', text: '看过' },
  doing: { icon: 'download', text: '在看' },
  on_hold: { icon: 'info', text: '搁置' },
  dropped: { icon: 'cancel', text: '弃置' },
  default: { icon: 'circle', text: '' },
  1: { icon: 'waiting', text: '想看' },
  2: { icon: 'success', text: '看过' },
  3: { icon: 'download', text: '在看' },
  4: { icon: 'info', text: '搁置' },
  5: { icon: 'cancel', text: '弃置' }
};

Component({
  properties: {
    itemData: {
      type: Object,
      value: {}
    },
    imageUrl: {
      type: String,
      value: ''
    },
    nameCn: {
      type: String,
      value: '默认中文名称'
    },
    name: {
      type: String,
      value: 'デフォルトの日本語名'
    },
    nameDisabled: {
      type: Boolean,
      value: false
    },
    infoItems: {
      type: Array,
      value: []
    },
    rank: {
      type: String,
      value: ''
    },
    ratingScore: {
      type: Number,
      value: null
    },
    ratingTotal: {
      type: String,
      value: ''
    },
    ratingDisabled: {
      type: Boolean,
      value: false
    },
    isPrivate: {
      type: Boolean,
      value: false
    },
    collection: {
      type: String,
      value: 'default'
    },
    collectionDisabled: {
      type: Boolean,
      value: false
    },
    roast: {
      type: String,
      value: ''
    }
  },

  observers: {
    'imageUrl'(imageUrl) {
      this.setData({ 
        actualImageUrl: imageUrl || DEFAULT_IMAGE_URL 
      });
    },
    'collection'(collection) {
      if (!this.properties.collectionDisabled) {
        this.updateCollectionDisplay(collection);
      }
    },
    'nameCn, name'(nameCn, name) {
      if (!nameCn || nameCn === '默认中文名称') {
        this.setData({
          nameCn: name,
          nameDisabled: true
        });
      }
    }
  },
  
  data: {
    actualImageUrl: DEFAULT_IMAGE_URL,
    collectionIcon: '',
    collectionText: ''
  },
  
  lifetimes: {
    attached() {
      if (!this.properties.collectionDisabled) {
        this.updateCollectionDisplay(this.properties.collection);
      }

      if (!this.properties.nameCn || this.properties.nameCn === '默认中文名称') {
        this.setData({
          nameCn: this.properties.name,
          nameDisabled: true
        });
      }
    }
  },
  
  methods: {
    ImageError() {
      this.setData({ actualImageUrl: DEFAULT_IMAGE_URL });
    },

    updateCollectionDisplay(status) {
      const { icon, text } = STATUS_MAP[status] || STATUS_MAP.default;
      this.setData({ collectionIcon: icon, collectionText: text });
    },

    onCardTap() {
      this.triggerEvent('click', { itemData: this.properties.itemData });
    },

    onCollectionTap() {
      if (this.properties.collectionDisabled) return;
      this.triggerEvent('collect', { itemData: this.properties.itemData });
    }
  }
});